package com.demo.io.file;

import java.io.*;
import java.net.InetAddress;
import java.net.Socket;

/**
 * @author : YU.TAN
 * @date : 2021/11/15 14:58
 */
public class TcpClientDemo1 {
    public static void main(String[] args) throws Exception {

        //建立连接
        Socket socket=new Socket(InetAddress.getByName("127.0.0.1"),9010);
        //创建输出流
        OutputStream outputStream = socket.getOutputStream();
        //读取文件
        FileInputStream fileInputStream = new FileInputStream(new File("C:\\Users\\谭羽\\Desktop\\新建文件夹\\2021\\11\\09\\images\\f73304f53a5e42dfa81477ad6fa9a64d.JPG"));

        byte[] bytes=new byte[1024];
        int len;

        while ((len=fileInputStream.read(bytes))!=-1){
            outputStream.write(bytes,0,len);
        }

        //通知服务器传输结束
        socket.shutdownOutput();

        //确定服务器接收完毕在断开连接
        InputStream inputStream = socket.getInputStream();
        ByteArrayOutputStream bos = new ByteArrayOutputStream();

        byte[] bytes1=new byte[1024];
        while ((len=inputStream.read(bytes1))!=-1){
            bos.write(bytes1,0,len);
        }

        System.out.println(bos.toString());


        //5.释放资源
        bos.close();
        inputStream.close();
        fileInputStream.close();
        outputStream.close();
        socket.close();
    }
}
